<?php

require_once(dirname(__FILE__) . "/../config/environment.php");
require_module("namelink");

$parameters =& $_GET;
if(!$parameters) $parameters =& $_POST;

$url = @urldecode($parameters["url"]);
$callback = @$parameters["callback"];
$open_tag = htmlspecialchars_decode(urldecode(@$parameters["open_tag"]));
$close_tag = @$parameters["close_tag"];

$request_uri = $_SERVER["REQUEST_URI"];
if(preg_match("/[&\?]url=(.*)$/", $request_uri, $arr)) $url = $arr[1];

if(!$open_tag) $open_tag = null;
if(!$close_tag) $close_tag = null;

$open_tag = str_replace("\'", "'", $open_tag);
$open_tag = str_replace('\"', '"', $open_tag);
$close_tag = str_replace("\'", "'", $close_tag);
$close_tag = str_replace('\"', '"', $close_tag);

$html = "";
if(!$url)
{
    $html = "";
}else
{
    $html = get_remote_page($url);
    if(!$callback) $html = NameLink::replace_relative_paths($html, $url);
    
    $markit = new NameTag($html, $open_tag, $close_tag);
    
    $html = $markit->markup_html();
}



if($callback)
{
    $json = array("html" => $html);
    echo "$callback(".json_encode($json).")";
}else
{
    echo $html;
}

?>